$(function () {
    //初始化 编辑页码标题
    PageUtil.initEditPageTitle("活动修改")

    //初始化 下拉框值
    SelectUtil.initSelectList("roleList", "/bflow/extRes/getRoleSelectList", true);
    SelectUtil.initSelectList("userList", "/bflow/extRes/getUserSelectList", true);

    // 初始化表单数据
    initInputFormData();

    $("input[name=assigneeType]").on("click", function () {
        var assigneeType = $(this).val();
        log(assigneeType);
        if(assigneeType == "USER_ID"){
            $("#assignee").show().val('').attr("readonly", "readonly");
            $("#userList_chosen").show().trigger("change");
            $("#userList").trigger("change");
            $("#roleList_chosen").hide();
        }else if(assigneeType == "ROLE_KEY"){
            $("#assignee").show().val('').attr("readonly", "readonly");
            $("#roleList_chosen").show();
            $("#roleList").trigger("change");
            $("#userList_chosen").hide();
        }else if(assigneeType == "ASSIGN_USER_ID" || assigneeType == "ASSIGN_ROLE_KEY"){
            $("#assignee").hide().val('');
            $("#roleList_chosen,#userList_chosen").hide();
        }
    });

    $("#userList,#roleList").on("change", function () {
        var val = $(this).val();
        $("#assignee").val(val)
    });


    // 表单验证 http://1000hz.github.io/bootstrap-validator/
    $('#inputForm').validator().on('submit', function (e) {
        if (e.isDefaultPrevented()) {
            // handle the invalid form...
        } else {
            //防止重复提交
            FormUtil.blockRepeatSubmit($('#inputForm'), 2000);

            var paramArr = $("#inputForm").serialize()
            HttpUtil.ajaxAsyncJsonPost("/bflow/act/save", paramArr, function(message){
                dialogTipSuccess(message);
                refreshPage(1500, '/static/bflow/ui/define/actList.html');
            });
            return false;
        }
    })

});

// 初始化表单数据
function initInputFormData(){
    var id = getBrowserQueryVar("id");
    console.log("id",id);
    if(CommUtil.isNotEmpty(id)){
        HttpUtil.ajaxAsyncJsonPost("/bflow/act/get", {id: id}, function(data){
            FormUtil.bindFormData("inputForm", data);
            $("#assignee").val(data.assignee);
            if(data.assigneeType == "USER_ID"){
                $("#userList option[value="+data.assignee+"]").attr("selected", "selected").trigger("chosen:updated");;
            }else if(data.assigneeType == "ROLE_KEY"){
                $("#roleList option[value="+data.assignee+"]").attr("selected", "selected").trigger("chosen:updated");;
            }
        });
    }
}

